<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统设置</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .tab-description {
            margin: 15px 0;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 4px;
            border-left: 4px solid #007bff;
        }
        
        /* 设置表单样式 */
        .settings-form {
            max-width: 600px;
            margin: 0 auto;
        }
        
        .settings-form h3 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }
        
        .form-group {
            margin-bottom: 15px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        
        .form-control {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }
        
        .form-control:focus {
            border-color: #007bff;
            outline: none;
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
        }
        
        .btn-save {
            width: 100%;
            padding: 12px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .btn-save:hover {
            background-color: #218838;
        }
        
        .save-message {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            text-align: center;
            display: none;
        }
        
        .save-message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .save-message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .btn-back {
            display: inline-block;
            margin-bottom: 20px;
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        
        .btn-back:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="index.html" class="btn-back">← 返回主页</a>
        <h1>系统设置</h1>
        
        <div class="tab-description">
            <p>系统配置和参数设置。</p>
        </div>
        <div class="settings-form">
            <h3>系统参数设置</h3>
            <form id="settingsForm">
                <div class="form-group">
                    <label for="param1">参数1 - 数据采集间隔(秒):</label>
                    <input type="text" id="param1" name="param1" class="form-control" placeholder="默认值: 30">
                </div>
                
                <div class="form-group">
                    <label for="param2">参数2 - 温度单位:</label>
                    <input type="text" id="param2" name="param2" class="form-control" placeholder="默认值: Celsius">
                </div>
                
                <div class="form-group">
                    <label for="param3">参数3 - 最大报警温度:</label>
                    <input type="text" id="param3" name="param3" class="form-control" placeholder="默认值: 80">
                </div>
                
                <div class="form-group">
                    <label for="param4">参数4 - 最小报警温度:</label>
                    <input type="text" id="param4" name="param4" class="form-control" placeholder="默认值: -10">
                </div>
                
                <div class="form-group">
                    <label for="param5">参数5 - 数据保留天数:</label>
                    <input type="text" id="param5" name="param5" class="form-control" placeholder="默认值: 30">
                </div>
                
                <div class="form-group">
                    <label for="param6">参数6 - 图表刷新间隔(秒):</label>
                    <input type="text" id="param6" name="param6" class="form-control" placeholder="默认值: 60">
                </div>
                
                <div class="form-group">
                    <label for="param7">参数7 - 报警邮箱:</label>
                    <input type="text" id="param7" name="param7" class="form-control" placeholder="默认值: admin@example.com">
                </div>
                
                <div class="form-group">
                    <label for="param8">参数8 - 日志级别:</label>
                    <input type="text" id="param8" name="param8" class="form-control" placeholder="默认值: INFO">
                </div>
                
                <div class="form-group">
                    <label for="param9">参数9 - 传感器名称:</label>
                    <input type="text" id="param9" name="param9" class="form-control" placeholder="默认值: TemperatureSensor1">
                </div>
                
                <div class="form-group">
                    <label for="param10">参数10 - 位置标识:</label>
                    <input type="text" id="param10" name="param10" class="form-control" placeholder="默认值: Room1">
                </div>
                
                <div class="form-group">
                    <button type="button" onclick="saveSettings()" class="btn-save">保存设置</button>
                </div>
            </form>
            <div id="saveMessage" class="save-message"></div>
        </div>
    </div>

    <script>
        // 保存设置函数
        function saveSettings() {
            // 收集表单数据
            const settings = {};
            for (let i = 1; i <= 10; i++) {
                const element = document.getElementById(`param${i}`);
                settings[`param${i}`] = element.value;
            }
            
            // 显示保存中消息
            const saveMessage = document.getElementById('saveMessage');
            saveMessage.textContent = '正在保存设置...';
            saveMessage.className = 'save-message';
            saveMessage.style.display = 'block';
            
            // 发送数据到服务器保存
            fetch('save_settings.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(settings)
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    saveMessage.textContent = '设置保存成功！';
                    saveMessage.className = 'save-message success';
                } else {
                    saveMessage.textContent = '保存失败: ' + data.message;
                    saveMessage.className = 'save-message error';
                }
            })
            .catch(error => {
                saveMessage.textContent = '保存失败: ' + error.message;
                saveMessage.className = 'save-message error';
            });
        }
        
        // 页面加载时加载现有设置
        window.addEventListener('load', function() {
            fetch('get_settings.php')
                .then(response => response.json())
                .then(data => {
                    if (data.status === 'success') {
                        // 填充表单
                        for (let i = 1; i <= 10; i++) {
                            const element = document.getElementById(`param${i}`);
                            if (element && data.settings[`param${i}`] !== undefined) {
                                element.value = data.settings[`param${i}`];
                            }
                        }
                    }
                })
                .catch(error => {
                    console.log('加载设置失败:', error);
                });
        });
    </script>
</body>
</html>